package com.dingding.study.service.注解方案.payImpl;

import com.dingding.study.annotation.PayCode;
import com.dingding.study.service.注解方案.IPay;
import org.apache.commons.collections.MapUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;
import org.thymeleaf.expression.Lists;

import java.util.*;


@Service
public class PayService1 implements ApplicationListener<ContextRefreshedEvent> {

    private static Map<String, IPay> payMap = null;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext();
        Map<String, Object> map = applicationContext.getBeansWithAnnotation(PayCode.class);
        if (MapUtils.isNotEmpty(map)){
            payMap = new HashMap<>();
            map.forEach((k,v) -> {
                String bizType = v.getClass().getAnnotation(PayCode.class).value();
                payMap.put(bizType, (IPay) v);
            });
        }
    }

    public void pay(String code) {
        System.out.println("执行时间");
        payMap.get(code).pay();
    }


}
